package com.lg.desireplayer.bandinfo;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lg.desireplayer.R;
import com.lg.desireplayer.bandlist.BandList;
import com.lg.desireplayer.db.BandListMaker;
import com.lg.desireplayer.main.Main;

public class AddBandinfo extends Activity{
	private EditText eBandName;
	private EditText eTitle;
	private EditText eUrl;
	private EditText eInfo;
	private Button bImgFileSrc;
	private Button bMusFileSrc;
	private Button bUpLoad;

	private TextView tImgFilePath;
	private TextView tMusFilePath;
	
	private static String sBandName="";
	private static String sTitle="";
	private static String sUrl="";
	private static String sInfo="";
	private static String img_absolutePath;
	private static String mus_absolutePath;


	
	File img_file;	
	
	
	
	
	
	
	HttpClient httpclient1;
	HttpClient httpclient2;
	
	
	HttpPost httppost1;
	HttpPost httppost2;
	
	MultipartEntity reqEntity1;
	MultipartEntity reqEntity2;
	
	Intent intent;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layoutresid);
		
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		.detectDiskReads().detectDiskWrites().detectNetwork()
		.penaltyLog().build());
		
		eTitle=(EditText)findViewById(R.id.AddBandinfo_EditText_title);
		eUrl=(EditText)findViewById(R.id.AddBandinfo_EditText_URL);
		eInfo=(EditText)findViewById(R.id.AddBandinfo_EditText_bandinfo);
		eBandName=(EditText)findViewById(R.id.AddBandinfo_EditText_bandname);
		bImgFileSrc=(Button)findViewById(R.id.AddBandinfo_Button_fileSource);
		bMusFileSrc=(Button)findViewById(R.id.AddBandinfo_Button_mp3Source);
		
		bUpLoad=(Button)findViewById(R.id.AddBandinfo_Button_fileUpload);
		tImgFilePath=(TextView)findViewById(R.id.AddBandinfo_text_FilePath);
		tMusFilePath=(TextView)findViewById(R.id.AddBandinfo_text_mp3Path);
		eBandName.setText(sBandName);
		eTitle.setText(sTitle);
		eUrl.setText(sUrl);
		eInfo.setText(sInfo);
		tImgFilePath.setText(img_absolutePath);
		tMusFilePath.setText(mus_absolutePath);
		
		 
		 	Intent intent1;
		    intent1=getIntent();
		    if(intent1.hasExtra("mu_path")){
		    	mus_absolutePath=intent1.getStringExtra("mu_path");
		    	Log.i("path1",mus_absolutePath);
		    	
		    	tMusFilePath.setText(mus_absolutePath);
		    	tImgFilePath.setText(img_absolutePath);
				
				
		    	
		    }		
	
		//////////////////////////////////db구현부///////////////////////////////
		
		bImgFileSrc.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				
				
				sBandName=eBandName.getText().toString();
				sTitle=eTitle.getText().toString();
				sUrl=eUrl.getText().toString();
				sInfo=eInfo.getText().toString();
				
				
				
				
				
				
				    intent= new Intent();


			        intent.setType("image/*");

			        intent.setAction(Intent.ACTION_GET_CONTENT);

			        startActivityForResult(Intent.createChooser(intent, "Select Picture"),0);
			        //				
//				
//				
			}
		});
		
		bMusFileSrc.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				sBandName=eBandName.getText().toString();
				sTitle=eTitle.getText().toString();
				sUrl=eUrl.getText().toString();
				sInfo=eInfo.getText().toString();
				    intent= new Intent(AddBandinfo.this,AndroidExplorerActivity.class);

				    intent.setAction(Intent.ACTION_GET_CONTENT);
				    
				   
				    startActivity(intent);
			
			     
			}
		});
		
		bUpLoad.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				int new_mem;
				BandListMaker.buildBandInfoList();
				new_mem=BandList.getSize();
				String cnt;
				Log.i("idex", Integer.toString(new_mem));
				cnt=Integer.toString(new_mem);
				Log.i("img_absolutePath",img_absolutePath);
				
				sBandName=eBandName.getText().toString();
				sTitle=eTitle.getText().toString();
				sUrl=eUrl.getText().toString();
				sInfo=eInfo.getText().toString();
				
				
				HttpClient httpclient1 = new DefaultHttpClient();
				HttpClient httpclient2 = new DefaultHttpClient();
				HttpPost httppost1 = new HttpPost("http://192.168.0.131/upload_image.php");
				HttpPost httppost2 = new HttpPost("http://192.168.0.131/upload_media.php");
				
				MultipartEntity reqEntity1 = new MultipartEntity();
				MultipartEntity reqEntity2 = new MultipartEntity();
				File file = new File(img_absolutePath);	
				reqEntity1.addPart("file", new FileBody(file));
				try {
					reqEntity1.addPart("index",new StringBody(cnt));
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				reqEntity2.addPart("file",new FileBody(new File(mus_absolutePath)));
				try {
					reqEntity2.addPart("index",new StringBody(cnt));
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				httppost1.setEntity(reqEntity1);
				httppost2.setEntity(reqEntity2);

				try {
					HttpResponse response1 = httpclient1.execute(httppost1);
					HttpResponse reesponse2 = httpclient2.execute(httppost2);
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					httppost1.abort();
					httppost2.abort();
				}
				

				StringBuffer sb = new StringBuffer(
						"http://192.168.0.131:8080/com.lg.project.band.cmd.BandInsertCmd.vcc?name=");
						sb.append(sBandName);
						sb.append("&title=");
						sb.append(sTitle);
						sb.append("&descr=");
						sb.append(sInfo);
						sb.append("&image=");
						sb.append("http://192.168.0.131/images/"+cnt+".jpg");
						sb.append("&media=");
						sb.append("http://192.168.0.131/media/"+cnt+".mp3");
						sb.append("&link=");
						sb.append(sUrl);
						
				try {
					URL url = new URL(sb.toString());

					url.openStream();

				} catch (MalformedURLException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
				Intent intent2main=new Intent(AddBandinfo.this, Main.class);
				startActivity(intent2main);
				

			}


		});
		
		
		
	}
	

	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		
		
		
		Log.i("res",""+resultCode);
		

		
		if (resultCode == RESULT_OK) {
			 
			 switch(requestCode) {
			 case 0:
				 Uri selPhotoUri = data.getData();
					Cursor c = getContentResolver().query(Uri.parse(selPhotoUri.toString()), null,null,null,null);
					c.moveToNext();
					img_absolutePath = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));
					
					img_file=new File(img_absolutePath);
					Log.i("img_file",img_absolutePath);
					
					

			  break;
			 case 1:
			
		 }
		}
		else if(resultCode== RESULT_CANCELED){
		return;
		}
			 super.onActivityResult(requestCode, resultCode, data);
	}
			
			
		
	}
	


	
	

